/*
 * SvrClient.h
 *
 *  Created on: 2012-7-11
 *      Author: ThinkPad
 */

#ifndef SVRCLIENT_H_
#define SVRCLIENT_H_

#include "SvrConfig.h"
#include "user_token.h"
#include "http.h"
#include "SvrTask.h"
#include "Svr_log.h"
#include "SvrFileManage.h"

/*
*op buf len
*/
#define op_buf_len 0xffff
/*
*op_buf
*/
struct op_buf
{
    ubyte * buf;
    size_t len;

    op_buf()
    {
        buf = NULL;
        len = 0;
    }
};

/*
*svr client connect type
*/
enum Client_Type
{
    INVALID_TYPE,
    /*go on receive*/
    ON_RECV_TYPE,
    /*browse http reuqest*/
    HTTP_BROWSE,
    /*admin server client*/
    HTTP_ADMIN_CLIENT,
    /*CDN autheration client*/
    CDN_AUTH_CLIENT,
    /*CDN node client*/
    CDN_NODE_HEARTBEAT,
    /*svr heartbeat*/
    SVR_HEARTBEAT
};

/*
* sessoin auth status
*/
enum AUTH_STATUS{AUTH_WAIT,AUTH_DISCONN_WAIT,AUTH_SUCCESS,AUTH_FAILURE};

/**
 * SvrClient class
 * auth function
 */
class SvrClient
{
public:
        SvrClient(void);
        ~SvrClient(void);

public:
        /*
        *SvrClient init
        */
        int  init(SvrConfig * pconfig,SvrTask * psvrtask,Svr_log * psvrlog,SvrFileManage * psvrfilemanage,int init_size = 100);

        /*
        *token resource
        */
        void alloc(user_token ** client);
        void free(user_token * client);
        int  get_free_count();
        int  get_count();

        /*
        *http request handler
        */
        Client_Type anylsis_client_type(char * buf,size_t buf_len,user_token * token);

        /*
         * run function
         * run
         * */
        void run(void);

protected:
        /*
        *http request header buf
        */
        void alloc_request_header(http_request_header ** hrt);
        void free_request_header(http_request_header * hrt);

        /*
        *op buf alloc and free
        */
        void alloc_op_buf(op_buf ** pbf);
        void free_op_buf(op_buf * pbf);

        /*
        *open reqeust file
        */
        bool open_request_file(http_request_header * hrt,user_token * token);
        /*
        *decode file name
        */
        bool decode_file_name(http_request_header * hrt,user_token * token);

        /*
        *decode pars
        */
        bool decode_pars(http_request_header * hrt,user_token * token);

        /*
        *decode administrator key
        */
        bool decode_private_key(http_request_header * hrt);

        /*
		* encode private key
		*/
	    bool encode_private_key(char * data, size_t data_len,size_t max_buf_len);

        /*
        *administrator operation cmd
        */
        Client_Type  administrator_op_cmd(http_request_header *hrt);

        /**
         * set request content offset
         */
        void set_request_content_offset(http_request_header * hrt,user_token * token);

private:
        /*
        *use token
        */
        stack<user_token *> client_pool;
        smart_lock pool_lock;
        int count;

        /*
        *http request data
        */
        stack<http_request_header *> http_request_header_pool;
        smart_lock http_lock;

        /*
        *op tempt buf
        */
        stack<op_buf * > op_buf_pool;
        smart_lock op_buf_lock;


        /*
        *svr config
        */
        SvrConfig  * pSvrconfig;
        SvrTask    * pSvrtask;
        Svr_log    * pSvrlog;
        SvrFileManage * pSvrFileUtils;

        /**
         * time tick
         */
        int client_time_tick;
};

#endif /* SVRCLIENT_H_ */
